﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class ProductManage_RFProductManage_Manage_ProductType_AirtightnessManage : System.Web.UI.Page
{
    private ProductType_Airtightness pa;
    public string seachFactor
    {
        get { return ViewState["SeachFactor"].ToString(); }
        set { ViewState["SeachFactor"] = value; }
    }

    public ProductManage_RFProductManage_Manage_ProductType_AirtightnessManage()
    {
        pa = new ProductType_Airtightness();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            seachFactor = string.Empty;
            BindGV();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        seachFactor = this.TextBox1.Text.Trim();
        BindGV();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        seachFactor = string.Empty;
        BindGV();
    }

    protected void BindGV()
    {
        if (string.IsNullOrEmpty(seachFactor))
            this.GridView1.DataSource = pa.LoadEntities();
        else
            GridView1.DataSource = pa.ProductType_Airtightness_GetbyPID(seachFactor);
        GridView1.DataBind();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindGV();
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        BindGV();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[11].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(GridView1.DataKeys[e.RowIndex].Values[2].ToString());
        pa.Delete(id);
        BindGV();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        BindGV();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        ProductType_AirtightnessInfo productTypeInfo = new ProductType_AirtightnessInfo();
        productTypeInfo.ID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[2].ToString());
        productTypeInfo.ProductTypeID = "";
        productTypeInfo.ChannelNo = 0;
        try
        {
            productTypeInfo.SpecValue = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim());
            productTypeInfo.InflateDuration = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim());
            productTypeInfo.BalanceDuration1 = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.Trim());
            productTypeInfo.BalanceDuration2 = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.Trim());
            productTypeInfo.MeasureDuration = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.Trim());
            productTypeInfo.LeakSpec = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.Trim());
            productTypeInfo.MinusLeakSpec = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text.Trim());
            productTypeInfo.ProductCubage = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[9].Controls[0])).Text.Trim());
        }
        catch(FormatException)
        {
            string str1 = "规格值输入不正确，请检查！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            return;
        }

        pa.Update(productTypeInfo);
        GridView1.EditIndex = -1;
        BindGV();
    }

    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        DetailsView d = (DetailsView)this.GridView1.Controls[0].Controls[0].FindControl("DetailsView1");
        ProductType_AirtightnessInfo productTypeInfo = new ProductType_AirtightnessInfo();
        productTypeInfo.ProductTypeID = ((TextBox)(d.Rows[0].Cells[1].Controls[0])).Text.Trim();
        try
        {
            productTypeInfo.ChannelNo = int.Parse(((TextBox)(d.Rows[1].Cells[1].Controls[0])).Text.Trim());
            productTypeInfo.SpecValue = int.Parse(((TextBox)(d.Rows[2].Cells[1].Controls[0])).Text.Trim());
            productTypeInfo.InflateDuration = int.Parse(((TextBox)(d.Rows[3].Cells[1].Controls[0])).Text.Trim());
            productTypeInfo.BalanceDuration1 = int.Parse(((TextBox)(d.Rows[4].Cells[1].Controls[0])).Text.Trim());
            productTypeInfo.BalanceDuration2 = int.Parse(((TextBox)(d.Rows[5].Cells[1].Controls[0])).Text.Trim());
            productTypeInfo.MeasureDuration = int.Parse(((TextBox)(d.Rows[6].Cells[1].Controls[0])).Text.Trim());
            productTypeInfo.LeakSpec = int.Parse(((TextBox)(d.Rows[7].Cells[1].Controls[0])).Text.Trim());
            productTypeInfo.MinusLeakSpec = int.Parse(((TextBox)(d.Rows[8].Cells[1].Controls[0])).Text.Trim());
            productTypeInfo.ProductCubage = int.Parse(((TextBox)(d.Rows[9].Cells[1].Controls[0])).Text.Trim());
        }
        catch(FormatException) 
        {
            string str1 = "规格值输入不正确，请检查！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            return;
        }

        ProductType p = new ProductType();
        ProductTypeInfo pi = p.LoadEntity(productTypeInfo.ProductTypeID);
        if (string.IsNullOrEmpty(pi.ProductTypeID))
        {
            string str1 = "此产品类型不存在，必须是在产品类型信息中录入过的产品！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            ProductType_AirtightnessInfo pro = pa.ProductType_Airtightness_GetbyPIDCHNO(productTypeInfo.ProductTypeID, productTypeInfo.ChannelNo);
            if (!string.IsNullOrEmpty(pro.ProductTypeID))
            {
                string str1 = "加不了啦，此产品类型在此通道的气密信息已存在！";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            }
            else
            {
                pa.Insert(productTypeInfo);
                string str1 = "添加成功！";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
                BindGV();
            }
        }
    }

    protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        BindGV();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }
}
